<?php
use Base\AdminController;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\NoReturn;

class PasswordController extends AdminController
{
    protected string $table = 'passwords';

    protected string $model = "password";

    #[NoReturn] public function init(): void
    {
        $this->routeWhiteLst = ['password@index','password@add','password@edit','password@del'];
        parent::init();
    }

    /**
     * 获取列表页数据
     * @return void
     */
    #[NoReturn] public function indexAction(): void
    {
        try {
            $sql = PasswordModel::query()->when($this->includes,function ($query,$value) {
                $query->with($value);
            })->where($this->where);
            $data = $sql->orderBy("id","desc")->paginate(Request::input("limit",10),[
                'id','title','username','password','remark','created_at'],NULL,Request::input("p",1));
            $result = [
                'items' => collect($data->items())->toArray(),
                'total'=>$data->total(),
                'per_page' => $data->perPage(),
                'page' => $data->currentPage()
            ];
            foreach ($result['items'] as $key => $item) {
                $result['items'][$key]['password'] = "•••••••••";
            }
            $this->apiSuccess($result,"获取成功",20000);
        }catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

}
